//
//  ZYPaymentRecordsViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/30.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYPaymentRecordsViewController.h"
#import "ZYHomeBillTableViewCell.h"
#import "ZYTodayBillInfoModel.h"
#import "XRRefreshHeader.h"
#import "XRRefreshFooter.h"
#import "ZYBillInfoMode.h"

@interface ZYPaymentRecordsViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) ZYTodayBillInfoModel *infoModel;

@property (nonatomic, strong) NSMutableArray *list;

@property (nonatomic) NSInteger page;

@end

@implementation ZYPaymentRecordsViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self configViews];
    [self configData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

#pragma mark - Setup View / Data

- (void)configViews {
    self.title = @"收款记录";
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYHomeBillTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYHomeBillTableViewCell"];
    
    @weakify(self)
    self.tableView.mj_header = [XRRefreshHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self getNewData];
    }];
    
    self.tableView.mj_footer = [XRRefreshFooter footerWithRefreshingBlock:^{
        @strongify(self)
        [self getMoreData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)configData {
}

- (void)getNewData {
    NSDictionary *dict = @{@"managerId": STR(ZYUserInfoModel.sharedInfoModel.userModel.managerId),
                           @"userId": STR(ZYUserInfoModel.sharedInfoModel.userModel.userId),
                           @"page": STR_LONG(self.page),
                           @"size": @"10"};
    [[DataManager manager] postTodayBillInfoParam:dict block:^(XRCommonModel *model, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (model.isSuccess) {
            self.page = 1;
            self.infoModel = [ZYTodayBillInfoModel mj_objectWithKeyValues:model.data];
            [self.list removeAllObjects];
            [self.list addObjectsFromArray:self.infoModel.billInfoResponse];
            if (self.infoModel.billInfoResponse.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

- (void)getMoreData {
    NSDictionary *dict = @{@"managerId": STR(ZYUserInfoModel.sharedInfoModel.userModel.managerId),
                           @"userId": STR(ZYUserInfoModel.sharedInfoModel.userModel.userId),
                           @"page": STR_LONG(self.page),
                           @"size": @"10"};
    [[DataManager manager] postGetBillInfoParam:dict block:^(XRCommonModel *model, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (model.isSuccess) {
            self.page += 1;
            self.infoModel = [ZYTodayBillInfoModel mj_objectWithKeyValues:model.data];
            [self.list addObjectsFromArray:self.infoModel.billInfoResponse];
            
            if (self.infoModel.billInfoResponse.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}


#pragma mark - Observer
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYHomeBillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYHomeBillTableViewCell"];
    cell.model = self.list[indexPath.row];
    return cell;
}

#pragma mark - Observer

#pragma mark - Notification

#pragma mark - Event Response

#pragma mark - Override Methods

#pragma mark - Delegate

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Setter / Getter

- (NSMutableArray *)list {
    if (!_list) {
        _list = [[NSMutableArray alloc] init];
    }
    return _list;
}

- (ZYTodayBillInfoModel *)infoModel {
    if (!_infoModel) {
        _infoModel = [[ZYTodayBillInfoModel alloc] init];
    }
    return _infoModel;
}

#pragma mark - Network




@end
